﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.EHR.Library;
using ZX.BLL.Extend;
using ZX.Web.UI.Controls;
using System.Text;


using ZX.SYS.Library;
using System.Data;
using ZX.DAL.DBExtend;

namespace ZX.WebApp.DeskTop
{
    public partial class Set_DeskTop : BasePage
    {
        int i = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadSelfContent();
        }

        /// <summary>
        /// 创建拖动层模板
        /// </summary>
        /// <param name="itemid">DesktopItemID</param>
        /// <param name="itemname">ItemName</param>
        /// <param name="height">没有该属性则赋值空</param>
        /// <returns></returns>
        string CreateHtmlModel(string itemid, string itemname, string height)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<div class='mo' id='m" + i + "' DesktopItemID='" + itemid + "'");
            if (!height.Equals(string.Empty))
            {
                height = height.IndexOf("px") != -1 ? height : height + "px;";
                sb.Append("  style='height:" + height + "' ");
            }
            sb.Append("  />");
            // if (isdefault > 0) sb.Append(" IsDefalt='" + isdefault + "'> ");
            sb.Append("     <h1><span><img src='../Images/icon/hr.gif' />" + itemname);
            sb.Append("     </span><font onclick='del_div(this)'>隐藏</font></h1>");
            sb.Append("<div class='nr'></div></div>");
            return sb.ToString();
        }

        private void CreateHtml()
        {
            SysRoleDesktop userdeskTopItem = new SysRoleDesktop();
            BaseBussnies<SysRoleDesktop> bll = new BaseBussnies<SysRoleDesktop>();
            bll.entity = userdeskTopItem;

            /*string tempSql= @"
select RoleId from (
	select RoleId from SysUserRole where UserID=&userid
) a
where a.RoleID in
(
	select a.RoleId from SysRoleDesktop a,SysUserDesktop b where a.RoleDesktopID=b.RoleDesktopID and b.AddUser=&userid
)
";
            tempSql = tempSql.Replace("&userid", "'" + this.UserId.ToString() + "'");*/
            List<SysRoleDesktop> list = (List<SysRoleDesktop>)bll.GetEntityListByCondition<SysRoleDesktop>("RoleId in (select RoleId from SysUserRole where UserID='" + this.UserId + "') and (RoleDesktopID not in (select RoleDesktopID from SysUserDesktop where AddUser = '" + this.UserId + "'))");
            StringBuilder sbHtml = new StringBuilder();

            if (list != null && list.Count > 0)
            {
                foreach (SysRoleDesktop sysRoleDesktop in list)
                {
                    string height = sysRoleDesktop.Height;
                    string htmlTemp = CreateHtmlModel(sysRoleDesktop.RoleDesktopID.ToString(), sysRoleDesktop.DeskTopName, sysRoleDesktop.Height);
                    sbHtml.Append(htmlTemp);
                    i++;
                }
            }
            lblContent.Text = sbHtml.ToString();
        }

        private void LoadSelfContent()
        {
            CreateHtml();
            //获取模块具体栏位布局信息
            DataSet ds = new DataSet();
            string strsql = @"
                            select a.*,b.DeskTopName,b.Height,b.Src from SysUserDesktop a,SysRoleDesktop b 
                            where a.RoleDesktopID=b.RoleDesktopID ";
            strsql += " and a.AddUser='" + this.UserId + "' order by a.Sort asc";
            DbSql execsql = new DAL.DBExtend.DbSql();
            ds = execsql.Query(strsql);

            StringBuilder sbCenterHtml = new StringBuilder();
            StringBuilder sbRightHtml = new StringBuilder();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    string htmlTemp = CreateHtmlModel(row["RoleDesktopID"].ToString(), row["DeskTopName"].ToString(), row["Height"].ToString());
                    string region = row["Region"].ToString().Trim().ToLower();
                    switch (region)
                    {
                        case "center": sbCenterHtml.Append(htmlTemp); break;
                        case "right": sbRightHtml.Append(htmlTemp); break;
                    }
                    i++;
                }
                lblCenterModel.Text = sbCenterHtml.ToString();
                lblRightModel.Text = sbRightHtml.ToString();
            }
        }
    }
}

